package aplicacao;


import classesBD.Artigo;
import classesBD.Congresso;
import classesBD.Livro;
import classesBD.Mestrado;
import classesBD.Pessoa;
import classesBD.Tcc;
import classesBD.Tese;
import classesBD.tabela;
import java.awt.Component;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.Normalizer;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


public class funcoes {
  private static ArrayList<String> pesquisadores;
  private static ArrayList<String> journals;
  private static ArrayList<String> congressos;
  
  
  public static void informa(Component tela, String msg){
    JOptionPane.showMessageDialog(tela, msg);
  }
  
  public static void print(String msg){
    System.out.println(msg);
  }
  
  public static String quotedStr(String str){
    return "'" + str + "'";
  }
  
  public static String montaInsert(String tabela, String[] campos){
    String retorno= "INSERT INTO " + tabela + " VALUES("; 
    for(int i=0;i<campos.length;i++){
      if(campos[i] == null){
        retorno += "null";
      }else if(campos[i].equalsIgnoreCase("default")){
        retorno += campos[i];
      }else{
        retorno += funcoes.quotedStr(campos[i]);
      }
      if (i != campos.length-1){
        retorno += ",";
      }  
    }
    return retorno += ")";        
  }
  
  public static boolean carregaPesquisadores(String path){
    String linha = "";
    pesquisadores = new ArrayList();
    try {  
        FileReader reader = new FileReader(path);  
        BufferedReader leitor = new BufferedReader(reader);  

        while ((linha = leitor.readLine()) != null) {
          if (!linha.isEmpty()){
            StringTokenizer st = new StringTokenizer(linha, ";" );  
            String dados = st.nextToken();
            pesquisadores.add(dados);
          }  
        }  

        leitor.close();  
        reader.close();  
        return true;
    } catch (Exception e) {  
        return false; 
    }  
  }
  
  public static String dumpPesq(){
    String result = "";
    for(int i=0;i<pesquisadores.size();i++){
      result += pesquisadores.get(i) + '\n';
    }
    return result;
  }
  
  public static boolean pesqEstaNaLita(String nomePesq){
    return (pesquisadores.isEmpty()) || (pesquisadores.contains(nomePesq));
  }
  
  public static String boolToStr(boolean val){
    if(val){
      return "S";
    }else{
      return "N";
    }
  }

  public static boolean strToBool(String val){
    if (val.equalsIgnoreCase("S")){
      return true;
    }else{
      return false;
    }
  }
  
  public static tabela cria(String nome){
    switch (nome.toLowerCase()){
      case "article": 
      case "inproceedings": return new Artigo();
      case "proceedings": return new Congresso();
      case "masterthesis": return new Mestrado();
      case "phdthesis": return new Tese();
      case "book": return new Livro();
      default: return null;
    }
  }
 
  public static String removeAcentos(String str) {
    str = Normalizer.normalize(str, Normalizer.Form.NFD);
    str = str.replaceAll("[^\\p{ASCII}|\n]", "");
    return str;
 
  }
  
  
  public static int getNumeroMes(String mesExtenso){
    switch(mesExtenso.toLowerCase()){
      case "january": return 1;
      case "february": return 2;
      case "march": return 3;
      case "april": return 4;  
      case "may": return 5;  
      case "june": return 6;
      case "july": return 7;
      case "august": return 8;
      case "september": return 9;
      case "october": return 10;
      case "november": return 11;  
      case "december": return 12;  
      default: return 0;  
    }
  }
  
  public static boolean isNullOrEmpty(String str){
    if(str == null){
      return true;
    }else if (str.isEmpty()){
      return true;
    }else{
      return false;
    }  
  }
 
  public static boolean temAutor(tabela tb){
    return ((tb instanceof Artigo) | (tb instanceof Mestrado) | (tb instanceof Tese) | (tb instanceof Livro) | (tb instanceof Tcc));
  }
  
  public static boolean listaVazia (){
    return pesquisadores.isEmpty();
  }
  
  public static void log(String texto){
    String fileName = "log.txt";  
    BufferedReader br;  
    try {
      br = new BufferedReader(new  FileReader(fileName));
     
      StringBuilder sb = new StringBuilder();  
      String text;
      while((text = br.readLine()) != null){  
        sb.append(text + '\n');  
      } 
      sb.append(texto + '\n');
      br.close();  
      new FileOutputStream(fileName).write(sb.toString().getBytes());
    }catch (IOException ex) {
      Logger.getLogger(funcoes.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  
  
  
}
